import store from './index'
import request from '@/utils/drawutils/request'

export default {
    state: {
        projectid: "",          //当前操作的项目id编号，这是一个后台生成的随机号
        project_id: -1,         //项目表的自动递增号
        projectExist: false,    //当前操作的项目id编号是否存在
        projectdescr: "",       //项目描述
        prefix: "",             //项目的前缀
        projects: [],           //项目列表

        loginname: "",          //登录名称
        dbid: 0,                //项目所在的数据平面

        operators: [],
        operpower: -1,
        opername: "",
        operid: -1,

    },
    mutations: {
        //项目编号
        setProjectId( state, projectid ){
            state.projectid = projectid
            getProjectExist( state, projectid )
            getAllProjects( state )
        },

        setProjectExist( state, projectExist ){
            state.projectExist = projectExist
        },

        setLoginName( state, loginname ){
            state.loginname = loginname
        },

        setPrefix( state, prefix ){
            state.prefix = prefix
        },

        setProjectDescr( state, projectdescr ){
            state.projectdescr = projectdescr
        },

        setOperPower( state, operpower ){
            state.operpower = operpower
        },

        setOperName( state, opername ){
            state.opername = opername
        },
        setOperId( state, operid ){
            state.operid = operid
        },

        //根据名称取图形的内容
        loadDrwFromLocalStorage( state, localStorageName ){
            let drawobj = JSON.parse( localStorage.getItem( localStorageName ) )
            // console.log( "load", drawobj )
            if( drawobj && drawobj.drawstr ){
              store.commit('loaddrw', drawobj.drawstr )
              store.commit('clearInit', { componentData: store.state.componentData } )
            }
            if( drawobj && drawobj.projectid ){
                store.commit('setProjectId', drawobj.projectid )
                
            }
            if( drawobj && drawobj.loginname ){
                state.loginname = drawobj.loginname
            }
        }
        

    },        

}

//判断项目编码是否存在
async function  getProjectExist( state, projectid ){  
    let res =  await request.post( "/drwproject/exist?projectid=" + projectid )
    console.log( "get project exist ", res )
    if( res.code === '1' ){
        state.projectExist = true 
        console.log( "projectexist:",   state.projectExist )

        state.project_id =  res.data[0].id 
        state.projectid =  res.data[0].projectid
        state.projectdescr =  res.data[0].descr
        state.prefix =  res.data[0].prefix
        state.dbid = res.data[0].dbid

        // console.log( state.project_id, state.projectid, state.projectdescr, state.prefix )
        console.log( res.data )

        request.get( "/savedb/getalarm?project_id=" + res.data[0].id ).then( res=>{
            store.commit('addAlarmArray', res.data  )  
        })

        request.get( "/savedb/getinfo?project_id=" + res.data[0].id ).then( res=>{
            for( var i=0; i<res.data.length; i++ ){
              store.commit('addInfo', res.data[i]  )  
            }
        })

        // request.get( "/hg3000operator/get?projectprefix=" + state.prefix ).then( res=>{
        //     // console.log("get opertaors:",  res.data )
        //     state.operators = res.data
        // })
        request.get( "/hg9000operator/getalloper" ).then( res=>{
            // console.log("get opertaors:",  res.data )
            state.operators = res.data
        })
    }
    else
        state.projectExist = false
    // store.commit( "setProjectExist", false )
    // console.log( "projectexist:  ", state.projectExist )
}

//搜索所有的项目
async function  getAllProjects( state ){  
    let res =  await request.post( "/drwproject/getallprojects" )
    console.log( "get all projects ", res )
    if( res.code === '1' ){
        state.projects = res.data
    }
}